<?php


namespace Biz\model\advertisement;

use exception\DbException;
use Medoo\Medoo;

class MPositionAdRule extends MBaseDbModel
{
    const _CONFIG_FILE_ = 'db_position';

    const _CONFIG_SELECT_ = 'db_position';

    const _DATABASE_ = 'hhz_position';

    const _TABLE_NAME = 'position_ad_rule';

    const IS_DELETED_YES = 1;
    const IS_DELETED_NO = 0;

    const RULE_TYPE_ASSIGN_SHARE = 'ad_type_assign_share';
    const RULE_TYPE_BIND_LIMIT = 'ad_type_bind_limit';
    const RULE_TYPE_SHARE_LIMIT = 'ad_type_share_limit';
    const RULE_TYPE_UNIQUE      = 'ad_type_unique';
    const RULE_TYPE_CONFLICT      = 'ad_type_conflict';
    const RULE_TYPE_MAP         = [
        self::RULE_TYPE_ASSIGN_SHARE => '强制共享状态', //radio 1 强制共享 2 强制不共享
        self::RULE_TYPE_BIND_LIMIT   => '使用部门数量限制', //text 整数输入
        self::RULE_TYPE_SHARE_LIMIT  => '共享数量限制',//text 整数输入
        self::RULE_TYPE_UNIQUE       => '互斥关键词',//radio 1 开启
        self::RULE_TYPE_CONFLICT     => '互斥资源位',//radio 1 开启
    ];

    public static function uniqueAd()
    {
        $ids = MPositionAdRule::select('id', ['rule_type' => MPositionAdRule::RULE_TYPE_UNIQUE, 'is_deleted' => MPositionAdRule::IS_DELETED_NO]);
        return empty($ids) ? [] : MPositionAdRuleDetail::select('ad_type_id', ['ad_rule_id' => $ids, 'is_deleted' => MPositionAdRuleDetail::IS_DELETED_NO]);
    }

    public static function addAndReturnDetail($info)
    {
        self::insert([
            'rule_type' => $info['rule_type'],
            'rule_value' => $info['rule_value'],
            'is_deleted' => self::IS_DELETED_NO,
        ]);

        $id = self::id();

        if (!$id) {
            DbException::writeFailed();
        }

        return array_merge($info, ['id' => $id]);
    }

    public static function edit($id, $data)
    {
        if (!$id) {
            return;
        }

        $updateData = [
            'rule_type' => $data['rule_type'],
            'rule_value' => $data['rule_value'],
        ];

        self::update($updateData, ['id' => $id]);
    }


    public static function getAll($page = 1, $perPage = 20)
    {
        $where = [
            self::_TABLE_NAME . '.is_deleted' => self::IS_DELETED_NO,
            MPositionAdRuleDetail::_TABLE_NAME . '.is_deleted' => self::IS_DELETED_NO,
            'GROUP' => ["ad_rule_id"],
            'LIMIT' => [($page - 1) * $perPage, $perPage],
        ];

        return self::select(
            [
                "[>]" . MPositionAdRuleDetail::_TABLE_NAME => ["id" => "ad_rule_id"],
                "[>]" . MPositionAdType::_TABLE_NAME => [MPositionAdRuleDetail::_TABLE_NAME . '.ad_type_id' => 'id'],
            ],
            [
                self::_TABLE_NAME . '.id',
                self::_TABLE_NAME . '.rule_type',
                self::_TABLE_NAME . '.rule_value',
                self::_TABLE_NAME . '.created_at',
                self::_TABLE_NAME . '.updated_at',
                'ad_type_names' => Medoo::raw('group_concat(' . MPositionAdType::_TABLE_NAME . '.type_name)'),
                'ad_type_id' => Medoo::raw('group_concat(' . MPositionAdType::_TABLE_NAME . '.id)'),
            ],
            $where);
    }

    public static function getTotalNum()
    {
        $where = [
            self::_TABLE_NAME . '.is_deleted' => self::IS_DELETED_NO,
        ];

        return self::count($where);
    }
}
